package com.googlecode.fahview.v6project.panels;

/*
 * #%L
 * This file is part of FAHView-v6project.
 * %%
 * Copyright (C) 2011 - 2013 Michael Thomas <mikepthomas@outlook.com>
 * %%
 * FAHView is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * %
 * FAHView is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * %
 * You should have received a copy of the GNU General Public License
 * along with FAHView.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import com.googlecode.fahview.v6project.model.QueueImpl;
import com.googlecode.fahview.v6project.utilities.ProgressCellRenderer;
import com.googlecode.fahview.v6project.utilities.QueueReader;
import javax.swing.JTable;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;


/**
 * Top component which displays something.
 *
 * @author <a href="mailto:mikepthomas@outlook.com">Michael Thomas</a>
 * @version $Id: $Id
 */
@ConvertAsProperties(
    dtd="-//com.googlecode.fahview.v6project.panels//Queue//EN",
    autostore=false
)
@TopComponent.Description(
    preferredID="QueueTopComponent",
    iconBase="com/googlecode/fahview/v6project/queue.png",
    persistenceType=TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "com.googlecode.fahview.v6project.panels.QueueTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
    displayName = "#CTL_QueueAction",
    preferredID="QueueTopComponent"
)
@Messages({
    "CTL_QueueAction=Queue",
    "CTL_QueueTopComponent=Queue Window",
    "HINT_QueueTopComponent=This is a Queue window"
})
public final class QueueTopComponent extends TopComponent {
    /**
     * <p>Constructor for QueueTopComponent.</p>
     */
    public QueueTopComponent() {
        initComponents();
        setName(Bundle.CTL_QueueTopComponent());
        setToolTipText(Bundle.HINT_QueueTopComponent());

        table.getColumn("Progress").setCellRenderer(new ProgressCellRenderer());
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        queueWindow = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"0", null, "0%", null},
                {"1", null, "0%", null},
                {"2", null, "0%", null},
                {"3", null, "0%", null},
                {"4", null, "0%", null},
                {"5", null, "0%", null},
                {"6", null, "0%", null},
                {"7", null, "0%", null},
                {"8", null, "0%", null},
                {"9", null, "0%", null}
            },
            new String [] {
                "Position", "Status", "Progress", "Project"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        table.setDoubleBuffered(true);
        queueWindow.setViewportView(table);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(queueWindow, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 607, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(queueWindow, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 181, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(113, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane queueWindow;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables

    /** {@inheritDoc} */
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
        // create queue
                QueueImpl queue = new QueueImpl(new QueueReader("/Users/mike/SkyDrive/Documents/University/Year 3/Computing Project/FAH/Core1/queue.dat"));
                System.out.println(queue);

                // select the current index in table
                int current = queue.getCurrent();
                getTable().getSelectionModel().setSelectionInterval(current, current);

                // Fill the table
                for (int i = 0; i <= 9; i++) {
                    int status = queue.getQueueIndex(i).getStat();
                    String percentage = queue.getQueueIndex(i).getWuid().getPercentage();
                    int proj = queue.getQueueIndex(i).getWuid().getProj();
                    int run = queue.getQueueIndex(i).getWuid().getRun();
                    int clone = queue.getQueueIndex(i).getWuid().getClone();
                    int gen = queue.getQueueIndex(i).getWuid().getGen();
                    String project =
                            proj + " (Run "
                            + run + ", Clone "
                            + clone + ", Gen "
                            + gen + ")";
                    setStatus(i, status);
                    setPercentage(i, percentage);
                    setProject(i, project);
                }
    }

    /** {@inheritDoc} */
    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    /**
     * <p>writeProperties.</p>
     *
     * @param p a {@link java.util.Properties} object.
     */
    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }
    /**
     * <p>readProperties.</p>
     *
     * @param p a {@link java.util.Properties} object.
     */
    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    /**
     * Get this QueuePanel table instance
     *
     * @return JTable table
     */
    public JTable getTable() {
        return table;
    }

    /**
     * Set the Status value in the QueuePanel table to a specified value at the
     * required position
     *
     * @param position location in the table to change value.
     * @param value the value to change to
     */
    protected void setStatus(int position, int value) {
        table.getModel().setValueAt(value, position, 1);
    }

    /**
     * Set the Percentage value in the QueuePanel table to a specified value at the
     * required position
     *
     * @param position location in the table to change value.
     * @param value the value to change to
     */
    protected void setPercentage(int position, String value) {
        table.getModel().setValueAt(value, position, 2);
    }

    /**
     * Set the Project value in the QueuePanel table to a specified value at the
     * required position
     *
     * @param position location in the table to change value.
     * @param value the value to change to
     */
    protected void setProject(int position, String value) {
        table.getModel().setValueAt(value, position, 3);
    }
}
